#include "Sort.h"

Sort::Sort(int arr[], int length)
{
	_array = arr;
	_length = length;
}

Sort::~Sort(void)
{
}

void Sort::quickSort()
{
	quicksort(_array, _length);
}


void Sort::quicksort(int arr[], int length)
{
	if(!arr || length < 1)
		return;
	else if(length == 2)
	{
		if (arr[0] > arr[1])
		{
			int t = arr[0];
			arr[0] = arr[1];
			arr[1] = t;
		}
		return;
	}


	int left = 0;
	int right = length - 1;
	int flag = arr[length / 2];
	while(left < right)
	{
		while (arr[left] < flag && left < right)
			left++;
		while (arr[right] > flag && left < right)
			right--;

		if (left < right)
		{
			int t = arr[left];
			arr[left] = arr[right];
			arr[right] = t;
			left++;
			right--;
		}
	}

	quicksort(arr, right+1);
	quicksort(arr+left, length-left);
}